import pgobject
import sys_server
import timer
from pgcontainer import *
from message import *
from util import *

class PgWidget(pgobject.PgObject):
    def __init__(self, id = -1, background = Color.BLUE,
                 foreground = Color.RED, rect = (0,0,0,0)):
        pgobject.PgObject.__init__(self, id, background, foreground, rect)

    def on_lose_focus(self):
        print 'PgWidget : lose focus '

    def on_get_focus(self):
        print 'PgWidget : get focus'
        
    def on_draw(self, canvas):
        print 'PgWidget : draw'
        self.draw_client(canvas)

    def on_key(self, msg):
        print 'PgWidget : key'

    def on_lbuttondown(self, msg):
        print 'PgWidget : lbuttondown'

    msg_map = {MSG_LOSE_FOCUS : on_lose_focus,
               MSG_GET_FOCUS : on_get_focus,
               MSG_DRAW  : on_draw,
               MSG_KEY   : on_key,
               MSG_LBUTTONDOWN : on_lbuttondown
               }
    
##############################################################
class PgButton(PgWidget):
    def __init__(self, label = '', id = -1, background = Color.BLUE,
                 foreground = Color.RED, rect = (0,0,0,0)):
        PgWidget.__init__(self, id, background, foreground, rect)
        self.label = label
        self.is_mouseover = False
        
    def on_lose_focus(self):
        print 'PgButton id' + str(self.id) + ': lose focus'

    def on_get_focus(self):
        print 'PgButton id' + str(self.id) + ': get focus'
        
    def on_draw(self, canvas):
        print 'PgButton : draw'
        self.draw_client(canvas)
        x,y,w,h = self.rect
        if self.is_focused:
            canvas.fill_rect((x+2,y+2,w-4,h-4), Color.GREEN)
        elif self.is_mouseover:
            canvas.fill_rect((x+2,y+2,w-4,h-4), Color.YELLOW)
        canvas.draw_text(self.label, x+10, y+3, size = 20)
        
    def on_key(self, msg):
        print 'PgButton : key' + str(msg.value)

    def on_lbuttonup(self, msg):
        print 'PgButton : lbuttonup'
        self.background = Color.BLUE
        self.on_draw(sys_server)
        self.notify_parent()

    def on_lbuttondown(self, msg):
#        timer.get_timer_manager().start_timer(self, id(self), 500)
        self.background = Color.RED
        self.on_draw(sys_server)

    def on_mousemove(self, msg):
        print 'here'
        x,y,dx,dy = msg.value
        if self.is_contain((x,y)):
            self.is_mouseover = True
        else:
            self.is_mouseover = False

    def on_timer(self):
        timer.get_timer_manager().kill_timer(id(self))
        self.background = Color.BLUE
        self.on_draw(sys_server)

    msg_map = {MSG_LOSE_FOCUS : on_lose_focus,
               MSG_GET_FOCUS : on_get_focus,
               MSG_DRAW  : on_draw,
               MSG_KEY   : on_key,
               MSG_LBUTTONUP : on_lbuttonup,
               MSG_LBUTTONDOWN : on_lbuttondown,
               MSG_MOUSEMOVE : on_mousemove,
               MSG_TIMER : on_timer}
    
##################################################################
class PgTextInputBox(PgWidget):
    def __init__(self, id = -1, background = Color.WHITE,
                 foreground = Color.RED, rect = (0,0,0,0)):
        PgWidget.__init__(self, id, background, foreground, rect)
        self.text = ''
        self.pos = 0
        
    def on_key(self, msg):
        print 'WinTest : key'
        if msg.value >= key.A and msg.value <= key.Z:
            self.text = self.text + chr(msg.value)
        elif msg.value == key.BACKSPACE:
            self.text = self.text[:-1]
        self.pos = self.pos + 1

    def on_draw(self, canvas):
        print 'WinTest : draw'
        self.draw_client(canvas)
        x,y,w,h = self.rect
        canvas.draw_text(self.text, x+5, y+5, h-10)
        
    msg_map = {MSG_DRAW  : on_draw,
               MSG_KEY   : on_key}

if __name__ == '__main__':
    screen = get_screen()
    win = PgWindow(title = 'Window', rect = (50,50,500,400))
    screen.add_window(win)
    
    w1 = PgWidget(rect = (405,205,100,200), background = (1,1,0))
    win.add_widget(w1)
    w2 = PgWidget(rect = (200,205,200,100), background = Color.RED)
    win.add_widget(w2)
    bt1 = PgButton(label = 'OK', id = 1, rect = (105,100,80,36))
    win.add_widget(bt1)
    bt2 = PgButton(label = 'Cancel', id = 2, rect = (250,100,100,36))
    win.add_widget(bt2)
    wt = PgTextInputBox(rect = (50,300,350,80))
    win.add_widget(wt)

    sys_server.run()
